// pages/tool/card/index/index.js
const app = getApp();
// var base64 = require("../images/base64");
Page({

    /**
     * 页面的初始数据  status  1待提交  4待审核  5已通过  6未通过
     *  coupon_type 1券  2册 3卡
     */
    data: {
        cardlist: '',
        status: {1: '待提交', 4: '待审核', 5: '已完成', 6: '未通过'},
        tipIcon: {
            1: 'https://files.dianlinet.com/dianli/images/2020-04-20/16-31-45.jpg',
            3: 'https://files.dianlinet.com/dianli/images/2020-04-20/16-49-34.jpg',
            2: 'https://files.dianlinet.com/dianli/images/2020-04-20/16-49-47.jpg'
        },
        showDownloadType:false,
        shareid:'',
        buttons:['删除'],
        showtips:false
    },


    template: function () {
        wx.navigateTo({
            url: '/tools/pages/tool/card/make1/Template/Template'
        })
    },

    myinfo: function (index) {
        app.func.getPromise('/my/info')
            .then(([code, res]) => {
                if (code == 200) {
                        this.cardlist(index);
                }
            });
    },

    auth: function () {
        wx.switchTab({
            url: '/pages/about/about'
        })
    },


    //请求卡券列表
    cardlist: function (index) {
        app.func.getPromise('/cardtype/list')
            .then(([code, res]) => {
                if (index == 0) {
                    if (res.data.length == 0) {
                        this.template();
                    }
                }
                if (index == 1) {
                    this.setData({cardlist: res.data});
                }
            })
    },


    slideButtonTap(e) {
         let id = e.currentTarget.dataset.id;
         app.func.confirm('提示','卡券一旦删除，不可恢复，是否继续')
             .then(()=>{
                 this.deleleCard(id);
             }).catch(()=>{

         })
    },

    deleleCard(id){
        app.func.getPromise(`/card/delete/${id}`)
            .then(([code,res])=>{
                if(code == 200){
                    this.cardlist(1);
                    app.func.toastPromise('删除成功');
                }else{
                    app.func.toastPromise(res.message);
                }
            })
    },

    onLoad: function (options) {
        this.setData({
            slideButtons: [{
                type: 'warn',
                text: '删除',
                extClass: 'test',
                src: '/page/weui/cell/icon_del.svg', // icon的路径
            }],
        });


        this.myinfo(0);
    },


    onShow: function () {
        this.myinfo(1);
    },


    viewdetial: function (e) {
        let id = e.currentTarget.dataset.id;
        wx.navigateTo({
            url: '/tools/pages/tool/card/index/detail/detail?id=' + id
        });
    },

    submitcard: function (id) {
        app.func.getPromise('/cardtype/submitcard/' + id)
            .then(([code, res]) => {
                if (code == 200) {
                    this.cardlist(1);
                } else {
                    app.func.toastPromise(res.message)
                }
            })
    },

    onShowtips(){
        this.setData({
            showtips:false,
        })
    },

    //status 1预览卡券    2编辑卡券    3再次制卡
    operate: function (e) {
        let status = e.currentTarget.dataset.status;
        let id = e.currentTarget.dataset.id;
        if (status == 1) {
            wx.navigateTo({
                url: '/tools/pages/tool/card/index/preview/preview?id=' + id
            })
        }
        if (status == 2) {
            wx.navigateTo({
                url: '/tools/pages/tool/card/make1/setInfo/setInfo?editcardinfo=' + id
            })
        }
        if (status == 3) {
            wx.navigateTo({
                url: '/tools/pages/tool/card/make1/print/print?printid=' + id
            });
        }
        if (status == 4) {
            app.func.confirm('确认操作', '一旦提交，卡券信息将不能编辑，是否提交？')
                .then(() => {
                    this.submitcard(id);
                })
                .catch(() => {
                    // console.log('取消');
                })
        }
        if(status == 8){
            this.setData({
                netSrc:`${app.globalData.domain}/dianlinet/poster/index.html?id=${id}`,
                imgSrc:`${app.globalData.domain}/cs/cardtype/poster/${id}`,
                shareid:id,
                showtips:true,
                showDownloadType:true
            });
        }
    },










})
